const observer = new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
        const oldValue = mutation.oldValue;
        const newValue = mutation.target.textContent;
        if (oldValue !== newValue) {
            try {
                const sendButton = document.querySelectorAll('.toolbar-send.btn-pair.ng-star-inserted');
                if (sendButton[0]) {
                    const alreadyInsert = document.getElementById('insertCheckBox');
                    if (!alreadyInsert) {
                        const label = document.createElement('label');
                        const checkBox = document.createElement('input');
                        checkBox.id = 'insertCheckBox'
                        checkBox.type = 'checkbox';
                        checkBox.addEventListener('change', (et) => {
                            window._is_private_comment = et.target.checked;
                        })
                        label.appendChild(checkBox)
                        const span = document.createElement('span');
                        span.innerText = '私密评论'
                        label.appendChild(span)
                        label.style = 'margin: 0px 5px 0 10px;display: flex;'
                        sendButton[0].parentElement.insertBefore(label, sendButton[0])
                    }
                }
            } catch (error) {
                debugger;
                console.error(error)
            }
        }
    })
})
observer.observe(document.body, {
    characterDataOldValue: true,
    subtree: true,
    childList: true,
    characterData: true
})

var originalXHR = window.XMLHttpRequest;
function newXHR() {
    var xhr = new originalXHR();
    xhr.open = function (method, url) {
        if (
            (method === 'POST' && /api\\/ship\\/ideas\\/[0-9a-f]{24}\\/comment/.test(url)) ||
            (method === 'PUT' && /api\\/ship\\/ideas\\/[0-9a-f]{24}\\/comments\\/[0-9a-f]{24}/.test(url))) {
            var originalSend = this.send;
            this.send = function (data) {
                //修改请求数据体
                var originalData = JSON.parse(data);
                // console.log('originalData:',originalData)
                var modifiedData = Object.assign({}, { is_private: window._is_private_comment||false }, originalData);
                originalSend.call(this, JSON.stringify(modifiedData));
            };
        }
        originalXHR.prototype.open.apply(this, arguments);
    };
    xhr.onreadystatechange = function () {
        // console.log('拦截到XMLHttpRequest 响应');
        if (this._onreadystatechange) {
            this._onreadystatechange();
        }
    };
    return xhr;
}
window.XMLHttpRequest = newXHR;